package com.xy.tool.autoconfigure.profile;

import com.xy.tool.profile.ProfileConfig;
import com.xy.tool.profile.ProfileManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;

@EnableConfigurationProperties({ProfileProperties.class})
@Configuration
public class ProfileConfiguration {

    @Value("${spring.profiles.active}")
    private String activeProfile;
    @Autowired
    private ProfileProperties profileProperties;

    @Bean
    public ProfileManager profileManager() {
        String[] array;
        return new ProfileManager(ProfileConfig.builder()
                .activeProfile(activeProfile)
                .tendingToDevProfiles((array = profileProperties.getTendingToDev()) == null ? new ArrayList<>() : Arrays.asList(array))
                .tendingToProdProfiles((array = profileProperties.getTendingToProd()) == null ? new ArrayList<>() : Arrays.asList(array))
                .build());
    }

}
